﻿using System;
using GameFramework.Event;
using TowerDefence_Zombie.Data;

namespace TowerDefence_Zombie
{
    public class GuideCondition_UpgradeTower : GuideCondition_Base
    {
        private int _tarTowerId;

        private int _tarTowerStar;

        public GuideCondition_UpgradeTower(DRGuideCondition conditionInfo, Action conitionCompleteClick)
            : base(conditionInfo, conitionCompleteClick)
        {
            _tarTowerId = conditionInfo.ConditionParams_Int;
            _tarTowerStar = Convert.ToInt32(conditionInfo.ConditionParams_Str);
        }

        public override void StartCondition()
        {
            base.StartCondition();
            GameEntry.Event.Subscribe(UpgradeTowerStarEventArgs.EventId, OnTowerStarUpgrade);
            var towerStar = GameEntry.Data.GetData<DataPlayer>().GetTowerStar(_tarTowerId);
            if (towerStar >= _tarTowerStar)
            {
                IsCompleteFlag = true;
            }
        }

        private void OnTowerStarUpgrade(object sender, GameEventArgs e)
        {
            UpgradeTowerStarEventArgs ne = (UpgradeTowerStarEventArgs)e;
            if (ne == null)
                return;

            if (ne.TowerId == _tarTowerId && ne.TowerStar >= _tarTowerStar)
            {
                IsCompleteFlag = true;
            }
        }

        public override void Dispose()
        {
            GameEntry.Event.Unsubscribe(UpgradeTowerStarEventArgs.EventId, OnTowerStarUpgrade);
            base.Dispose();
        }
    }
}
